package com.xl98.controller;


import com.xl98.entity.*;
import com.xl98.service.GroupsService;
import com.xl98.service.UserGroupsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@Api(value="聊天室" ,tags = {"聊天室"})
@RestController
@RequestMapping("/groups")
public class GroupsController {
    @Autowired
    private GroupsService groupsService;
    @Autowired
    private UserGroupsService userGroupsService;

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有聊天室")
    public Result findAllGroups() {
        List<Groups> list = groupsService.selectAll();
        return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,list);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/keyword", method = RequestMethod.POST)
    @ApiOperation(value = "关键字查询聊天室")
    public Result findByKeyword(String keyword) {
        List<Groups> list = groupsService.selectByKeyword(keyword);
        return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,list);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectByAdminIdName", method = RequestMethod.POST)
    @ApiOperation(value = "根据群名和群主id查询聊天室")
    public Result selectByAdminIdName(String adminId, String name) {
        System.out.println(adminId);
        List<Groups> list = groupsService.selectByAdminIdName(adminId,name);
        return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,list);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "创建聊天室")
    public Result insertGroups(@RequestBody Groups groups){
        return ResultFactory.buildSuccessResult(groupsService.insert(groups));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改聊天室")
    public Result updateGroups(@RequestBody Groups groups){
        return ResultFactory.buildSuccessResult(groupsService.update(groups));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除聊天室")
    public Result deleteGroups(String id){
        return ResultFactory.buildSuccessResult(groupsService.deleteById(id));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectByName", method = RequestMethod.POST)
    @ApiOperation(value = "聊天室名查询聊天室")
    public Result selectByName(String name) {
        List<Groups> groups = groupsService.selectByName(name);
        return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,groups);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    @ApiOperation(value = "聊天室id查询聊天室")
    public Result selectById(String id) {
        Groups groups = groupsService.selectByID(id);
        return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,groups);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectByAdminId", method = RequestMethod.POST)
    @ApiOperation(value = "群主名查询聊天室")
    public Result selectByAdminId(String adminId) {
        List<Groups> groups = groupsService.selectByAdminId(adminId);
        return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,groups);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/addUserGroup", method = RequestMethod.POST)
    @ApiOperation(value = "用户加入聊天室")
    public Result addUserGroup(@RequestBody UserGroups userGroups) {
        if(userGroupsService.addUserGroups(userGroups)){
            return ResultFactory.buildSuccessResult("添加成功");
        }
        return ResultFactory.buildFailResult("添加失败");
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/deleteUserGroup", method = RequestMethod.POST)
    @ApiOperation(value = "用户退出聊天室")
    public Result deleteUserGroup(@RequestBody UserGroups userGroups) {
        if(userGroupsService.deleteUserGroups(userGroups)){
            return ResultFactory.buildSuccessResult("退出成功");
        }
        return ResultFactory.buildFailResult("退出失败");
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectByUserId", method = RequestMethod.POST)
    @ApiOperation(value = "根据成员id查询聊天室")
    public Result selectByUserId(String userId) {
        List<Groups> list = groupsService.selectByUserId(userId);
        return ResultFactory.buildSuccessResult(list);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectUserByGroupId", method = RequestMethod.POST)
    @ApiOperation(value = "根据id查询成员")
    public Result selectUserByGroupId(String groupId) {
        List<User> list = groupsService.selectUserById(groupId);
        return ResultFactory.buildSuccessResult(list);

    }

}

